<template>
  <view>
    <my-search @goSearch="goSearch"></my-search>
    <view class="scroll_view">
      <scroll-view scroll-y="true" class="scroll_left" :style="{height:winHeight+'px'}">
        <block v-for="(val,i) in cateList" :key="i">
          <view class="left_item" :class="{active:i==active}" @click="addAct(i)">
            {{val.cat_name}}
          </view>
        </block>
      </scroll-view>
      <scroll-view scroll-y="true" class="scroll_right" :style="{height:winHeight+'px'}" :scroll-top="scrollTop">
        <view class="right_box" v-for="(val,i) in cateLevel2" :key="i">
          <view class="right_item_title">
            /{{val.cat_name}}/
          </view>
          <view class="cateLevel3">
            <view class="item_level3" v-for="(val2,i2) in val.children" :key="i2" @click="naviToList(val2)">
              <image :src="val2.cat_icon" mode=""></image>
              <text>{{val2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import tabbarBadge from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [tabbarBadge],
    data() {
      return {
        winHeight: 0,
        cateList: [],
        active: 0,
        cateLevel2: [],
        scrollTop: 0
      };
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.winHeight = sysInfo.windowHeight - 50
      this.getList()
    },
    methods: {
      async getList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 获取二级分类
        this.cateLevel2 = res.message[0].children
      },
      addAct(index) {
        this.active = index
        this.cateLevel2 = this.cateList[index].children
        // 因为如果scrolltop前后一样的话，不会重置scrollTop的值
        this.scrollTop == 0 ? this.scrollTop = 1 : this.scrollTop = 0
      },
      naviToList(item) {
        uni.navigateTo({
          url: "/subpkg/goods_list/goods_list?cid=" + item.cat_id
        })
      },
      // 跳转到search界面
      goSearch() {
        uni.navigateTo({
          url: '/subp'
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .right_item_title {
    color: #cdasds;
    font-size: 12px;
    font-weight: bold;
    text-align: center;
    padding: 15px 0;
  }

  .scroll_view {
    display: flex;
  }

  .scroll_left {
    width: 120px;

    .left_item {
      background-color: #F7F7F7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      position: relative;

      &.active {
        background-color: #FFFFFF;

        &::before {
          content: "";
          display: block;
          height: 30px;
          width: 3px;
          position: absolute;
          background-color: #C00000;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }

      }
    }
  }

  .cateLevel3 {
    display: flex;
    flex-wrap: wrap;

    .item_level3 {
      width: 33.3%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;

      image {
        width: 60px;
        height: 60px;
      }

      text {
        font-size: 12px;
      }
    }
  }
</style>
